<template>
	<view class="register-container">
		<view class="form-container">
			<view class="form-item">
				<view class="icon-wrapper">
					<text class="iconfont">&#xe67b;</text>
				</view>
				<input type="number" placeholder="请输入手机号" v-model="phone" @input="handlePhoneInput" />
			</view>

			<view class="form-item">
				<view class="icon-wrapper">
					<text class="iconfont">&#xe67e;</text>
				</view>
				<input type="number" placeholder="请输入验证码" v-model="code" />
				<button class="code-btn" :disabled="!isPhoneValid || countdown > 0" @click="getCode">
					{{ countdown > 0 ? `${countdown}s` : '获取验证码' }}
				</button>
			</view>

			<view class="form-item">
				<view class="icon-wrapper">
					<text class="iconfont">&#xe682;</text>
				</view>
				<input type="password" placeholder="请输入密码" v-model="password" password="true" />
			</view>

			<view class="form-item">
				<view class="icon-wrapper">
					<text class="icon">&#xe682;</text>
				</view>
				<input type="password" placeholder="请再次输入密码" v-model="confirmPassword" password="true" />
			</view>
		</view>

		<button class="register-btn" @click="submitForm">确认注册</button>
		<view class="pswd">
			<navigator url="findPwd" hover-class="none">
				<view>忘记密码？</view>
			</navigator>
			<navigator url="login" hover-class="none">
				<view>已有账号?登录</view>
			</navigator>
		</view>

		<view class="agreement">
			<checkbox-group v-model="agreeTerms" @change="checks">
				<checkbox value="agree" class="checkbox"></checkbox>
			</checkbox-group>
			<view class="checkbox-label">
				我已阅读并同意
				<navigator url="/pages/setting/webview?type=1" class="link">《用户许可》</navigator>
				和
				<navigator url="/pages/setting/webview?type=2" class="link">《隐私协议》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	import api from '../../api/request';

	export default {
		data() {
			return {
				phone: '',
				code: '',
				password: '',
				confirmPassword: '',
				countdown: 0,
				isPhoneValid: false,
				openid: '',
				agreeTerms: []
			};
		},
		onLoad() {
			// 从缓存中获取openid
			this.openid = uni.getStorageSync('openid') || '';
		},
		computed: {
			canSubmit() {
				return this.isPhoneValid && this.code && this.password && this.confirmPassword && this.password && this
					.agreeTerms.includes('agree') === this
					.confirmPassword;
			}
		},
		methods: {
			handlePhoneInput(e) {
				const phone = e.detail.value;
				this.phone = phone;
				// 简单的手机号验证
				this.isPhoneValid = /^1[3-9]\d{9}$/.test(phone);
			},
			checks(e) {
				this.agreeTerms = e.detail.value;
				console.log('e', e.detail.value)
			},
			// 获取验证码
			getCode() {
				console.error('getCodegetCode');
				if (!this.isPhoneValid) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}
				// 1注册验证码 2忘记密码验证码 3修改密码验证码，4修改支付密码 5微信登录绑定手机号
				this.$api.getSmsCode({
						phone: this.phone,
						type: 1
					})
					.then(res => {
						uni.showToast({
							title: '验证码发送成功',
							icon: 'success'
						});
						// 开始倒计时
						this.countdown = 60;
						const timer = setInterval(() => {
							this.countdown--;
							if (this.countdown <= 0) {
								clearInterval(timer);
							}
						}, 1000);
					})
					.catch(error => {
						uni.showToast({
							title: error.msg || '获取验证码失败',
							icon: 'success'
						});
						console.error('获取验证码失败', error);
					});
			},



			// 提交表单
			submitForm() {
				var that = this;
				if (that.password !== that.confirmPassword) {
					uni.showToast({
						title: '两次密码输入不一致',
						icon: 'none'
					});
					return;
				}
				if (!that.agreeTerms.includes('agree')) {
					uni.showToast({
						title: '请阅读并同意隐私协议',
						icon: 'none'
					});
					return;
				}
				uni.login({
					provider: 'weixin',
					success: (res) => {
						that.wxCode = res.code;
						const registerData = {
							wx_code: that.wxCode,
							phone: that.phone,
							password: that.password,
							re_password: that.confirmPassword,
							code: that.code,
						};

						that.$api.wxchatReg(registerData)
							.then(res2 => {
								uni.hideLoading();
								if (res2.code === 1) {
									// 登录成功，存储token
									uni.setStorageSync('token', res2.data.token);
									uni.showToast({
										title: '注册成功',
										icon: 'success'
									});
									// 跳转到个人中心
									setTimeout(() => {
										uni.switchTab({
											url: '/pages/index/index'
										});
									}, 1500);
								} else {
									uni.showToast({
										title: res2.msg || '注册失败',
										icon: 'none'
									});
								}
							})
							.catch(error => {
								uni.hideLoading();
								console.error('注册失败', error);
								uni.showToast({
									title: error.msg,
									icon: 'none'
								});
							});


					},
					fail: () => {
						uni.showToast({
							title: '获取微信登录信息失败',
							icon: 'none'
						});
					}
				});

			},

			// 返回上一页
			navigateBack() {
				uni.navigateBack();
			}
		}
	};
</script>

<style scoped>
	.register-container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #fff;
	}

	.pswd {
		padding: 0 40rpx;
		line-height: 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		color: #666;
	}

	.agreement {
		margin: 120rpx 50rpx 0;
		display: flex;
		align-items: center;
		font-size: 26rpx;
		color: #666;
		position: fixed;
		bottom: 80rpx;
	}

	.checkbox-label {
		display: flex;
		align-items: center;
		padding-left: 20rpx;
	}

	.link {
		color: #e61e2b;
		text-decoration: underline;
		margin: 0 15rpx;
	}

	.form-container {
		padding: 40rpx;
	}

	.form-item {
		display: flex;
		align-items: center;
		border-bottom: 1rpx solid #e0e0e0;
		padding: 20rpx 0;
		position: relative;
	}

	.icon-wrapper {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #e61e2b;
	}

	.icon {
		font-family: 'iconfont';
		font-size: 32rpx;
	}

	input {
		flex: 1;
		height: 60rpx;
		font-size: 28rpx;
		padding-left: 20rpx;
	}

	.code-btn {
		width: 180rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		background-color: #e61e2b;
		color: #fff;
		font-size: 24rpx;
		border-radius: 25rpx;
		position: absolute;
		right: 0;
		z-index: 999999999;
	}

	.register-btn {
		margin: 60rpx 40rpx;
		height: 90rpx;
		line-height: 90rpx;
		background-color: #e61e2b;
		color: #fff;
		font-size: 32rpx;
		border-radius: 45rpx;
	}
</style>